package com.dingceng.demo.base

import android.app.Activity
import android.app.ActivityManager
import android.app.ActivityManager.RunningAppProcessInfo
import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import androidx.appcompat.app.AppCompatActivity
import androidx.multidex.MultiDexApplication
import com.dingceng.demo.base.utils.MmkvUtil
import com.dingceng.demo.base.utils.log
import java.util.*
import java.util.concurrent.CopyOnWriteArrayList

/**
 * 基类Application
 */
open class BaseApplication : MultiDexApplication() {
    private val mActivityName = LinkedList<String>()
    private val mActivity = LinkedList<Activity>()
    private var mTopActivity: AppCompatActivity? = null
    companion object {
        private lateinit var INSTANCE: BaseApplication
        fun getIns() = INSTANCE
    }

    override fun onCreate() {
        super.onCreate()
        INSTANCE = this
        MmkvUtil.init(this)
        registerActivityLifecycleCallbacks()
    }



    private fun registerActivityLifecycleCallbacks() {
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                mActivityName.add(activity.javaClass.name)
                mActivity.add(activity)
            }

            override fun onActivityStarted(activity: Activity) {

            }

            override fun onActivityResumed(activity: Activity) {
                if (activity is AppCompatActivity) {
                    mTopActivity = activity
                    log("BaseApplication", "onActivityResumed " + activity.javaClass.name)
                }
            }

            override fun onActivityPaused(activity: Activity) {

            }

            override fun onActivityStopped(activity: Activity) {
                log("BaseApplication", "onActivityStopped " + activity.javaClass.name)
            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {

            }

            override fun onActivityDestroyed(activity: Activity) {
                if (mTopActivity != null && mTopActivity!!.javaClass.name.endsWith(activity.javaClass.name)) {
                    mTopActivity = null
                }
                mActivityName.remove(activity.javaClass.name)
                mActivity.remove(activity)
                log("BaseApplication", "onActivityDestroyed " + activity.javaClass.name)
            }
        })
    }

    fun isBackground(): Boolean {
        val activityManager = applicationContext
            .getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val appProcesses = activityManager.runningAppProcesses
        for (appProcess in appProcesses) {
            if (appProcess.processName == getPackageName()) {
                log(
                    getPackageName(), "此appimportace ="
                            + appProcess.importance
                )
                return if (appProcess.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                    log(
                        getPackageName(), "处于后台"
                                + appProcess.processName
                    )
                    true
                } else {
                    log(
                        getPackageName(), "处于前台"
                                + appProcess.processName
                    )
                    false
                }
            }
        }
        return false
    }

    fun getTopActivity(): AppCompatActivity? {
        return mTopActivity
    }

    fun isExsitActivity(activityName: String): Boolean {
        for (name in mActivityName) {
            if (TextUtils.equals(name, activityName)) {
                return true
            }
        }
        return false
    }

    fun clearActivity() {
        if (mActivity.size > 0) {
            for (activity in CopyOnWriteArrayList<Activity>(mActivity)) {
                activity.finish()
            }
            mActivity.clear()
        }
    }


    var uuid :String?= ""




}